package crmdna.client.programtype;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import com.googlecode.objectify.Ref;
import com.googlecode.objectify.annotation.Cache;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.annotation.Load;

import crmdna.client.practice.PracticeEntity;
import crmdna.client.programtype.ProgramType.ProgramTypeProp;

@Entity @Cache
public class ProgramTypeEntity {
	@Id Long programTypeId;
	@Index String name;
	String displayName;
	
	@Index @Load Set<Ref<PracticeEntity>> practiceEntities = new HashSet<>();
	
	public ProgramTypeProp toProp() {
		ProgramTypeProp prop = new ProgramTypeProp();
		prop.programTypeId = programTypeId;
		prop.name = name;
		prop.displayName = displayName;
		
		for (Ref<PracticeEntity> ref : practiceEntities) 
			if (null != ref)
				prop.practiceProps.add(ref.get().toProp());
		
		Collections.sort(prop.practiceProps);
									
		return prop;
	}		
}
